import 'package:base/main.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:framework/main.dart';
import 'package:module_discover/enums/domain_type_enum.dart';
import 'package:module_discover/pages/filter_domain/filter_domain_logic.dart';
import 'package:module_discover/pages/filter_domain/view_model/select_input_vm.dart';
import 'package:module_discover/pages/model/filter_model.dart';

/// 选择类型

class ChildSelectWidget extends StatelessWidget {
  /// 传参
  final BaseFilterItem child;
  final String parentKey;

  /// 取参
  final FilterDomainLogic logic = Get.find<FilterDomainLogic>();
  late DomainTypeEnum domainType;

  ChildSelectWidget({
    super.key,
    required this.child,
    required this.parentKey,
  });

  @override
  Widget build(BuildContext context) {
    domainType = DomainTypeEnum.fromValue(child.type) ?? DomainTypeEnum.OTHER;
    return Row(
      children: [
        buildItemWidget(
          context: context,
          vm: logic.selectInputVM,
          value: logic.selectInputVM.selectedFromValue,
          isFrom: true,
        ),
        // 中间分隔线
        Expanded(
          child: Container(
            margin: const EdgeInsets.symmetric(horizontal: 10),
            child: const Divider(
              color: Colors.black,
              height: 0.5,
            ),
          ),
        ),
        // 结束值选择
        buildItemWidget(
          context: context,
          vm: logic.selectInputVM,
          value: logic.selectInputVM.selectedToValue,
          isFrom: false,
        ),
      ],
    );
  }

  Widget buildItemWidget({
    required BuildContext context,
    required SelectInputVM vm,
    required RxInt value,
    required bool isFrom,
  }) {
    return InkWell(
      onTap: () => _showNumberPicker(context, vm, value, isFrom),
      borderRadius: BorderRadius.circular(3),
      child: Container(
        width: 100,
        margin: const EdgeInsets.symmetric(vertical: Constants.px_8),
        padding: const EdgeInsets.symmetric(
          horizontal: Constants.px_10,
          vertical: Constants.px_4,
        ),
        decoration: BoxDecoration(
          color: JColor.color_f3f3f3,
          borderRadius: BorderRadius.circular(3),
        ),
        child: Row(
          children: [
            Obx(() => Expanded(
                  child: Center(
                    child: Text(
                      _getDisplayText(value.value, isFrom),
                      style: const TextStyle(
                        color: Colors.black,
                        fontSize: 10,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                  ),
                )),
            Icon(Icons.keyboard_arrow_down,
                size: 16, color: JColor.color_383838)
          ],
        ),
      ),
    );
  }

  // 数字选择弹窗
  void _showNumberPicker(
    BuildContext context,
    SelectInputVM vm,
    RxInt value,
    bool isFrom,
  ) {
    int tempValue = value.value == 0 ? 1 : value.value; // 处理默认值0的情况
    final scrollController = FixedExtentScrollController(
      initialItem: tempValue - 1,
    );

    showCupertinoModalPopup(
      context: context,
      builder: (_) => Container(
        height: 280,
        color: CupertinoColors.white,
        child: Column(
          children: [
            // 顶部操作栏
            Container(
              decoration: const BoxDecoration(
                border: Border(
                    bottom: BorderSide(color: CupertinoColors.systemGrey5)),
              ),
              child: Padding(
                padding:
                    const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    CupertinoButton(
                      padding: EdgeInsets.zero,
                      child: const Text('取消',
                          style: TextStyle(color: CupertinoColors.systemBlue)),
                      onPressed: () => Navigator.pop(context),
                    ),
                    CupertinoButton(
                      padding: EdgeInsets.zero,
                      child: const Text('确定',
                          style: TextStyle(color: CupertinoColors.systemBlue)),
                      onPressed: () {
                        isFrom
                            ? vm.selectedFromValue(tempValue)
                            : vm.updateToValue(tempValue);
                        logic.checkChildSelectValue(
                            domainType,
                            child.title ?? "",
                            parentKey,
                            child.key ?? "",
                            "${vm.selectedFromValue.value}-${vm.selectedToValue.value}",
                            "${vm.selectedFromValue.value}-${vm.selectedToValue.value}");
                        Navigator.pop(context);
                      },
                    ),
                  ],
                ),
              ),
            ),

            // 数字选择器
            Expanded(
              child: CupertinoPicker(
                itemExtent: 40,
                scrollController: scrollController,
                onSelectedItemChanged: (index) {
                  tempValue = index + 1; // 仅修改临时变量
                },
                children: List.generate(
                    40,
                    (index) => Center(
                          child: Text(
                            '${index + 1}',
                            style: const TextStyle(
                              fontSize: 22,
                              color: CupertinoColors.label,
                            ),
                          ),
                        )),
              ),
            )
          ],
        ),
      ),
    );
  }

  // 显示文本逻辑
  String _getDisplayText(int value, bool isFrom) {
    if (value == 0) return "无限";
    return isFrom ? "$value" : "$value";
  }
}
